package attend.action.holidays;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import attend.com.CodeMetrix;
import attend.com.Session;
import attend.dao.common.CommonDao;
import attend.dao.holidays.HolidaysDao;
import dao.Paging.PagingDAO;
import exception.BaseException;

public class HolidaysAction extends Session {
	
	private Map 							session;									//session
	
	private CommonDao 						commonDao;		//DAO
	
	private HolidaysDao						holidaysDao;	//DAO
	
	//페이징
	private PagingDAO						pDao;			//Paging DAO
	private int count		= 0;
	private int rowCount	= 0; 				// 검색한  건수
	private int cur_page	= 1; 				// 현재 페이지 
	
	
	private List<HashMap<String , String>>	list			=		new ArrayList<HashMap<String,String>>();
	private List<HashMap<String , String>>	listYear		=		new ArrayList<HashMap<String,String>>();
	
	private String 							syear	=	"ALL";			//년도
	private String 							smonth	=	"ALL";			//월
	
	private String 							date;					//휴일일자
	private String 							type;					//휴일유형
	private String 							etc;						//비고

	private String 							crud_type;				//CRUD 구분

	private String 							msg;					//message

	private int start_row;

	private int end_row;

	private String 							s_chk;
	
	private int								seq;					//휴일 sequence

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws BaseException, SQLException {

		holidaysDao 				= 				new HolidaysDao();
		
		//조회년도 <s:select />
		holidaysDao.retrieveYearList();
		
		setListYear(holidaysDao.getList());		//조회연도
		
		//syear	=	String.valueOf(java.util.Calendar.getInstance().get(Calendar.YEAR));
		
		return SUCCESS;
	}
	
	/**
	 * <pre>휴일 조회 결과</pre>
	 * @return
	 * @throws BaseException
	 */
	public String ajaxList() throws BaseException {
		
		System.out.println(syear);
		System.out.println(smonth);
		
		holidaysDao 	= 			new HolidaysDao();
		
		holidaysDao.retrieveHolidaysRowCount(syear, smonth);
		
		rowCount		=			holidaysDao.getRowCount();
		
		pDao			=			new PagingDAO(15 , 10 , rowCount , cur_page);
		
		start_row 		= 			pDao.getWriting_Start();			//시작 페이지 1
	    end_row   		= 			pDao.getWriting_End();				//끝페이지
		
		holidaysDao.retrieveAjaxList(syear , smonth , start_row , end_row);
		
		list = holidaysDao.getList();
		
		return SUCCESS;
	}
	
	/**
	 * <pre>휴일등록 팝업</pre>
	 * @return
	 * @throws BaseException
	 * @throws SQLException 
	 */
	public String openHolidaysAddPop() throws BaseException, SQLException {
		
		crud_type		=	CodeMetrix.INSERT;
		
		holidaysDao		=		new		HolidaysDao();
		commonDao		=		new 	CommonDao();
		
		date			=		commonDao.getDate(0);	//오늘날짜
		
		commonDao.retireveCommonCode("108");		//휴일유형 대표코드
		list			=		commonDao.getList();
		
		return SUCCESS;
	}
	
	/**
	 * <pre>휴일등록/수정</pre>
	 * @return
	 * @throws BaseException
	 */
	public String saveHolidaysReg() throws BaseException {
		
		holidaysDao	=	new HolidaysDao();
		
		holidaysDao.setCrud(crud_type);				//CRUD type 지정
		
		holidaysDao.saveHolidays(date , type , etc , seq);
		
		msg	=	holidaysDao.getMsg();
		
		System.out.println(msg);
		
		return SUCCESS;
	}
	
	/**
	 * <pre>체크된 휴일들을 삭제한다.</pre>
	 * @return
	 * @throws BaseException
	 */
	public String deleteHolidaysBySeq() throws BaseException {
		
		holidaysDao	=	new HolidaysDao();
		
		holidaysDao.deleteHolidaysBySeq(s_chk);
		
		msg	=	holidaysDao.getMsg();
		
		return SUCCESS;
	}
	
	/**
	 * <pre>휴일수정</pre>
	 * @return
	 * @throws BaseException
	 * @throws SQLException 
	 */
	public String updateHolidaysBySeq() throws BaseException, SQLException {
		
		crud_type	=	CodeMetrix.UPDATE;
		
		
		holidaysDao	=	new HolidaysDao();
		
		holidaysDao.retrieveHolidaysBySeq(seq);
		
		list		=	holidaysDao.getList();
		
		seq			=	Integer.valueOf(list.get(0).get("seq")) ;	//sequence
		date		=	list.get(0).get("date");
		type		=	list.get(0).get("type_cd");
		etc			=	list.get(0).get("type_nm");
		
		commonDao	=	new CommonDao();
		commonDao.retireveCommonCode("108");		//휴일유형 대표코드
		list			=		commonDao.getList();
		
		return SUCCESS;
	}
	
	/************************************************************************************************/
	//getter / setter
	public List<HashMap<String, String>> getListYear() {
		return listYear;
	}

	public void setListYear(List<HashMap<String, String>> listYear) {
		this.listYear = listYear;
	}

	public List<HashMap<String, String>> getList() {
		return list;
	}

	public void setList(List<HashMap<String, String>> list) {
		this.list = list;
	}

	public String getSyear() {
		return syear;
	}

	public void setSyear(String syear) {
		this.syear = syear;
	}

	public String getSmonth() {
		return smonth;
	}

	public void setSmonth(String smonth) {
		this.smonth = smonth;
	}

	public PagingDAO getPDao() {
		return pDao;
	}

	public void setPDao(PagingDAO dao) {
		pDao = dao;
	}

	public int getRowCount() {
		return rowCount;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}

	public int getCur_page() {
		return cur_page;
	}

	public void setCur_page(int cur_page) {
		this.cur_page = cur_page;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getEtc() {
		return etc;
	}

	public void setEtc(String etc) {
		this.etc = etc;
	}

	public String getCrud_type() {
		return crud_type;
	}

	public void setCrud_type(String crud_type) {
		this.crud_type = crud_type;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getS_chk() {
		return s_chk;
	}

	public void setS_chk(String s_chk) {
		this.s_chk = s_chk;
	}

	public int getSeq() {
		return seq;
	}

	public void setSeq(int seq) {
		this.seq = seq;
	}
	
}